package com.wsj.util.tool;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.wsj.web.constant.homeSiteNews.HomeSiteNewsConstant;

/*****
 * 
 * @author fengbin
 * 2008-12-27
 *
 */
public class HtmlCreateObjFile  implements Runnable
{

	private HtmlCreateStack htmlCreateBean;
	
    private Integer allPage;
	Integer pageSize = 100;
	
//	总的个数
	Integer allRows;
	
	public HtmlCreateObjFile()
	{
		
	}
	
//	构造函数,加入参数
	public HtmlCreateObjFile(HtmlCreateStack htmlCreateBean,Integer allPage,Integer allRows)
	{
		this.htmlCreateBean = htmlCreateBean;
		this.allPage = allPage;
		this.allRows = allRows;
	}

	
	public Integer getAllRows() 
	{
		return allRows;
	}

	public void setAllRows(Integer allRows) {
		this.allRows = allRows;
	}

	public Integer getAllPage() {
		return allPage;
	}

	public void setAllPage(Integer allPage) {
		this.allPage = allPage;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public HtmlCreateStack getHtmlCreateBean() {
		return htmlCreateBean;
	}

	public void setHtmlCreateBean(HtmlCreateStack htmlCreateBean) {
		this.htmlCreateBean = htmlCreateBean;
	}
	
	public HtmlCreateObjFile(HtmlCreateStack htmlCreateBean)
	{
		this.htmlCreateBean = htmlCreateBean;
	}
	
	
	 public void run()
	 {
		 
		 for(int j=1;j<=allPage;j++)
		 {
			 if(j!=allPage.intValue())
			 {
				 for(int cur=0;cur<pageSize;cur++)
				 {
					 try {
						HtmlFileBean htmlFileBean= htmlCreateBean.pop();
						 
						 StringBuffer[] str=null;
						 if(htmlFileBean.getStringBuffer()!=null)
							{
								str=htmlFileBean.getStringBuffer();	
							}
							
						 for(int end=0;end<str.length;end++)
						 {
							 String fileName=HomeSiteNewsConstant.homeSiteNewsHead+htmlFileBean.getId()+"_"+String.valueOf(end+1)+".html";
							 
							 //将字符串 生成静态页面
							  BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(htmlFileBean.getCreateFilePath()+fileName));
							  String resultStr=str[end].toString();
							  out.write(resultStr.getBytes("UTF-8"));
							  
							  
							  
//							清除缓冲
								if(out != null)
								{
									out.flush();
									out.close();
								}			
									
							   
						 }
					     
						 /*线程稍微休息几豪秒*/
                          Thread.sleep((int)(200));
							 
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 
				 }
			 }
			 else//最后一页
			 {
				 //获取最后页的个数
				 Integer endPageRows=(allPage*pageSize)-allRows;
				 for(int cur=0;cur<endPageRows;cur++)
				 {
					 try {
						HtmlFileBean htmlFileBean= htmlCreateBean.pop();
						 StringBuffer[] str=null;
						 if(htmlFileBean.getStringBuffer()!=null)
							{
								str=htmlFileBean.getStringBuffer();	
							}
							
						 for(int end=0;end<str.length;end++)
						 {
							 String fileName=HomeSiteNewsConstant.homeSiteNewsHead+htmlFileBean.getId()+"_"+String.valueOf(end+1)+".html";
							 
							 //将字符串 生成静态页面
							  BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(htmlFileBean.getCreateFilePath()+fileName));
							  String resultStr=str[end].toString();
							  out.write(resultStr.getBytes());
							   
						 }
						 
						 /*线程稍微休息几豪秒*/
						  Thread.sleep((int)(200));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				 }
			 }
		 }
		 
		 
	 }
	
	
}
